//
//  CommunityTableViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "CommunityTableViewController.h"
#import "CommunityTableViewCell.h"
#import "TopicTableViewController.h"
#import "ClickRightCornerJumpToViewController.h"
#import "MHDataHelper.h"


@interface CommunityTableViewController ()<UIPopoverControllerDelegate>

//存放模块标题和简单介绍
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *introduceArray;
@property(nonatomic,strong)NSMutableArray*headerPictureArray;

@property(nonatomic,strong)UIImageView *backgroundView;
@property (nonatomic,strong)NSMutableDictionary *topicNumbers;

@end

@implementation CommunityTableViewController

//懒加载数组
- (NSMutableDictionary *)topicNumbers{
    if (!_topicNumbers) {
        _topicNumbers = [NSMutableDictionary dictionary];
    }
    return _topicNumbers;
}

//懒加载数组
- (NSArray *)headerPictureArray{
    
    if (!_headerPictureArray) {
        
        _headerPictureArray = [NSMutableArray array];
    }
    return _headerPictureArray;
}

//懒加载数组
- (NSArray *)titleArray{
    if (!_titleArray) {
        
        _titleArray = [NSArray arrayWithObjects:@"热聊",@"唱歌",@"故事",@"心情",@"灌水",@"秘密", nil];
    }
    return _titleArray;
}

//懒加载数组
- (NSArray *)introduceArray{
    if (!_introduceArray) {
        
        _introduceArray = [NSArray arrayWithObjects:@"让热门的话题有你",@"让更多的人听到你",@"每个人都有自己的故事",@"此时的你，心情好吗",@"随便吹牛，聊聊人生", @"维多利亚的秘密",nil];
    }
    return _introduceArray;
}

//设置UITableViewStyle样式为Plain
- (instancetype)initWithStyle:(UITableViewStyle)style{
    
    if (self = [super initWithStyle:UITableViewStylePlain]) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSLog(@"这个页面是 社区--> CommunityTableViewController");
    //显示底部tabbar
    [self.tabBarController.tabBar setHidden:NO];


}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *array = @[@"热聊",@"唱歌",@"故事",@"心情",@"灌水",@"秘密"];
    for (int i = 0; i < 6; i ++) {
        [[MHDataHelper shareDataHelper] getNumbersOfFindModelInTopicWithArrayType:array[i] didFinished:^(NSInteger number) {
            NSLog(@"-------%ld",number);
            NSString *numbers = [NSString stringWithFormat:@"%ld",number];
            NSString *key = [NSString stringWithFormat:@"%d",i];
            [self.topicNumbers setObject:numbers forKey:key];
                
            [self.tableView reloadData];
        }];
    }
    
    
    NSLog(@"这个页面是 社区--> CommunityTableViewController");
    //设置标题
    self.navigationItem.title = @"社区";
    //设置右上方按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"🔔" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickRightBarAction:)];
    //注册cell
    [self.tableView registerClass:[CommunityTableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    //去掉界面横线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //添加数据
    [self addData];
    //添加背景图片
    [self addBackgroundPicture];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(recentlyRefresh)];
    
}

//下拉刷新
-(void)recentlyRefresh{
    if (self.topicNumbers.count) {
        [self.topicNumbers removeAllObjects];
    }
    
    NSArray *array = @[@"热聊",@"唱歌",@"故事",@"心情",@"灌水",@"秘密"];
    for (int i = 0; i < 6; i ++) {
        [[MHDataHelper shareDataHelper] getNumbersOfFindModelInTopicWithArrayType:array[i] didFinished:^(NSInteger number) {
            NSLog(@"-------%ld",number);
            NSString *numbers = [NSString stringWithFormat:@"%ld",number];
            NSString *key = [NSString stringWithFormat:@"%d",i];
            [self.topicNumbers setObject:numbers forKey:key];
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing ];
        }];
    }
    

}


//添加背景图片
- (void)addBackgroundPicture{
    
    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.backgroundView.image = [UIImage imageNamed:@"1112.jpg"];
    [self.view addSubview:self.backgroundView];
    
    //  创建需要的毛玻璃特效类型
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    //  毛玻璃view 视图
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    //添加到要有毛玻璃特效的控件中
    effectView.frame = self.backgroundView.bounds;
    //设置模糊透明度（范围0到1）
    effectView.alpha = 0.9f;
    [self.backgroundView addSubview:effectView];
    [self.view sendSubviewToBack:effectView];
    [self.view sendSubviewToBack:self.backgroundView];
    
    
}


//添加数据
- (void)addData{
    
    for (int i  = 2; i < 8; i++) {
        
        NSString *picturePath = [NSString stringWithFormat:@"%d.jpg",i];
        [self.headerPictureArray addObject:picturePath];
    }
    
}

//点击铃铛
- (void)clickRightBarAction:(UIBarButtonItem*)sender{
    
   ClickRightCornerJumpToViewController *clickRightCornerJumpVC = [[ClickRightCornerJumpToViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    
   [self showViewController:clickRightCornerJumpVC sender:nil];
    self.hidesBottomBarWhenPushed = NO;
}


#pragma mark - Table view data source

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//每个区cell的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.headerPictureArray.count;
}

//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.view.frame.size.height/7.5;
}

//创建cell并赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //cell的重用
    CommunityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    //如果没有再创建cell
    if (!cell) {
        cell = [[CommunityTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    cell.titleLbl.text = self.titleArray[indexPath.row];
    cell.introduceLbl.text = self.introduceArray[indexPath.row];
    NSString *path = [NSString stringWithFormat:@"%@",self.headerPictureArray[indexPath.row]];
    cell.imaV.image = [UIImage imageNamed:path];
    
    if (indexPath.row < _topicNumbers.count) {
        NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
        cell.numberLbl.text = [NSString stringWithFormat:@"%@个话题", _topicNumbers[key]];
    }
    

    
    return cell;
}

//cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"你点击了CommunityTableViewController中的cell");
    
    TopicTableViewController *topVC = [[TopicTableViewController alloc]init];
    
    //把当前模块的标题传到下个页面
    topVC.aString = self.titleArray[indexPath.row];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:topVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}



@end
